package balmysundaycandy.more.low.level.operations.datastore.impl;

import java.util.concurrent.Future;

import balmysundaycandy.core.future.ProtocolMessageFuture;
import balmysundaycandy.core.operations.OperationNamespaces.datastore_v3;
import balmysundaycandy.more.low.level.operations.datastore.DatastoreOperation;

import com.google.apphosting.api.ApiProxy;
import com.google.apphosting.api.ApiProxy.ApiConfig;
import com.google.apphosting.api.DatastorePb.Query;
import com.google.apphosting.api.DatastorePb.QueryExplanation;

/**
 * explain operation.
 * 
 * @author marblejenka
 */
public final class ExplainOperation extends DatastoreOperation<Query, QueryExplanation> {

	@Override
	public QueryExplanation call(Query request) {
		QueryExplanation queryExplanation = new QueryExplanation();
		boolean apiproxycallsucceed = queryExplanation.mergeFrom(ApiProxy.makeSyncCall(datastore_v3.packageName, datastore_v3.methodName.Explain, request.toByteArray()));
		if (!apiproxycallsucceed) {
			throw new RuntimeException("call " + getClass().getName() + " failed.");
		}
		return queryExplanation;
	}

	@Override
	public Future<QueryExplanation> callAsync(Query request, ApiConfig apiConfig) {
		QueryExplanation response = new QueryExplanation();
		return new ProtocolMessageFuture<QueryExplanation>(ApiProxy.makeAsyncCall(datastore_v3.packageName, datastore_v3.methodName.Explain, request.toByteArray(), apiConfig),
				response);
	}
}